<?php

require_once 'SmartL/Config.php';
require_once 'SmartL/Application.php';
require_once 'SmartL/Application/Login/Interface.php';
require_once 'SmartL/Zend/Auth/Adapter/Doctrine.php';
require_once 'SmartL/Zend/Form.php';
require_once 'Zend/Form.php';
require_once 'Db/Record/User.php';

class iDo_Login implements SmartL_Application_Login_Interface
{
    private static $_loginResult = null;
    private static $_loginForm = null;
	
    /**
     * @return Zend_Auth_Result
     */
    static public function getLoginResult ()
    {
        return self::$_loginResult;
    }
    
    /**
	 * @var Zend_Form
     */
    static public function getLoginForm()
    {
    	return self::$_loginForm;
    }

    public function login (Zend_Controller_Request_Abstract $request)
    {
        $adapter = null;
        // prepare adapter -----
        // login trought form		
        if (isset($_POST['loginAction'])) {
            $loginType = 'form';
            // create login form from configuration file
            $formConfig = new SmartL_Config();
            $formConfig->load(SmartL_Application::getInstance()->getBasePath() . "/Application/Config/Default/LoginForm.xml");   
            self::$_loginForm = $form = new Zend_Form($formConfig->form['login']->toArray());
            // validation			
            if ($form->isValid($_POST)) {
                // values of textboxes
                $form->populate($_POST);
                $values = $form->getValues();
                $username = $values['username'];
                $password = $values['password'];
                // authentication
                $adapter = new SmartL_Zend_Auth_Adapter_Doctrine('Db_Record_User', 'username', 'password');
                $adapter->setIdentity($username);
                $adapter->setCredential($password);
            }
        }
        // authenticate --------
        if (isset($adapter)) {
            $auth = Zend_Auth::getInstance();
            require_once 'Zend/Auth/Storage/Session.php';
            $auth->setStorage(new Zend_Auth_Storage_Session());
            $result = $auth->authenticate($adapter);
            self::$_loginResult = $result;
            if ($result->isValid()) {
                $resultData = null;
                switch ($loginType) {
                    case 'form':
                        $resultData = $adapter->getResultRowObject();
                }
                $auth->getStorage()->write($resultData);                
            }
        }
    }

    public function getUserRoles ()
    {
		return array( SmartL_Application_Login_Interface::LOGIN_ANONYMOUS );
    }
    public function getUserPreferences ()
    {
		return null;
    }
    
    public function getUserId()
    {
    	if (Zend_Auth::getInstance()->getIdentity() instanceof Db_Record_User) {
    		return Zend_Auth::getInstance()->getIdentity()->id;
    	}
    	else {
    		return null;
    	}
    }
}